/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.database;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @author Administrator
 */
public class OrgDao4SQLServer implements IOrgDao{
    
    private static Map<String,String> name2codes = new HashMap<String,String>();
    private Connection conn;

    public OrgDao4SQLServer(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Org org) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean doUpdate(Org org) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean doDelete(long[] pids) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Org findById(long pid) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Org> findAll(int beginIndex, int size, String orgCode) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SimpleOrganBean> findAllSimpleMine(String orgCode) throws Exception {
        List<SimpleOrganBean> all = new ArrayList<SimpleOrganBean>();
        PreparedStatement pstmt = null;
      
               
        String sql = "select orgcode,smallname FROM  org where len(orgcode)=8 and orgcode like '"+orgCode+"%'";
        
        try {            
            
            pstmt = this.conn.prepareStatement(sql);
           
            ResultSet rs = pstmt.executeQuery(); // 执行查询
            SimpleOrganBean simpleOrgan = null;
            while (rs.next()) { // 如果有查询的结果，则可以向下执行
                simpleOrgan = new SimpleOrganBean();
                simpleOrgan.setOrgCode(rs.getString(1));
                simpleOrgan.setSmallName(rs.getString(2));                
                
                all.add(simpleOrgan); // 向集合中插入内容
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return all;
    }
    
    public String getCode(String name) throws Exception{
        String orgCode = name2codes.get(name);        
        if(orgCode==null){
            orgCode = getCodeByName(name);
            if(orgCode!=null)
                name2codes.put(name, orgCode);
        }
        return orgCode;
    }
    
    public String getCodeByName(String name) throws Exception{
        String orgCode;
        PreparedStatement pstmt = null;
        String sql = "select orgcode FROM  org where smallname=?";
        
        try {            
            
            pstmt = this.conn.prepareStatement(sql);    
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery(); // 执行查询
            if(rs.next()){
                orgCode = rs.getString(1);
            }else
                return null;
           
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }        
        return orgCode;
    }
    
    
     public List<SimpleOrganBean> findAllSimpleOrg(String orgCode) throws Exception {
         List<SimpleOrganBean> all = new ArrayList<SimpleOrganBean>();
        PreparedStatement pstmt = null;
      
               
        String sql = "select orgcode,smallname FROM  org where len(orgcode)<=6 and orgcode like '"+orgCode+"%'";
        
        try {            
            
            pstmt = this.conn.prepareStatement(sql);
           
            ResultSet rs = pstmt.executeQuery(); // 执行查询
            SimpleOrganBean simpleOrgan = null;
            while (rs.next()) { // 如果有查询的结果，则可以向下执行
                simpleOrgan = new SimpleOrganBean();
                simpleOrgan.setOrgCode(rs.getString(1));
                simpleOrgan.setSmallName(rs.getString(2));                
                
                all.add(simpleOrgan); // 向集合中插入内容
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return all;
    }
    
      public List<SimpleOrganBean> findAllSimpleOrgnMine(String orgCode) throws Exception {
         List<SimpleOrganBean> all = new ArrayList<SimpleOrganBean>();
        PreparedStatement pstmt = null;
      
               
        String sql = "select orgcode,smallname FROM  org where orgCode like '"+orgCode+"%'";
        
        try {            
            
            pstmt = this.conn.prepareStatement(sql);
           
            ResultSet rs = pstmt.executeQuery(); // 执行查询
            SimpleOrganBean simpleOrgan = null;
            while (rs.next()) { // 如果有查询的结果，则可以向下执行
                simpleOrgan = new SimpleOrganBean();
                simpleOrgan.setOrgCode(rs.getString(1));
                simpleOrgan.setSmallName(rs.getString(2));                
                
                all.add(simpleOrgan); // 向集合中插入内容
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return all;
    }
     
    @Override
    public int getTotalSize() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
